/** 
 * DefaultMatchOfficialParser.java Created by iweerarathna on Aug 31, 2011.
 * 
 * Copyright (c) 2011 Virtusa Corporation. All Rights Reserved.<br><br>. 
 * 
 */
package org.uim.cricket.analyzer.parsers.match.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.uim.cricket.analyzer.parsers.Plogger;
import org.uim.cricket.analyzer.parsers.match.MatchOfficialParsable;
import org.uim.cricket.analyzer.parsers.misc.SimpleNameParsable;
import org.uim.cricket.analyzer.parsers.misc.URLIDParsable;
import org.uim.cricket.analyzer.parsers.misc.URLTitleParsable;
import org.uim.cricket.entities.Official;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.match.MatchOfficials;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

/**
 * TODO Class description goes here.
 * @version 
 * @author iweerarathna
 */
public class DefaultMatchOfficialParser implements MatchOfficialParsable {

    private URLIDParsable urlIdParser = null;
    private URLTitleParsable urlTitleParser = null;
    private SimpleNameParsable simpleNameParser = null;
    
    public DefaultMatchOfficialParser(URLIDParsable theUrlIdParser, URLTitleParsable theUrlTitleParser, SimpleNameParsable theSimpleNameParser) {
        this.urlIdParser = theUrlIdParser;
        this.urlTitleParser = theUrlTitleParser;
        this.simpleNameParser = theSimpleNameParser;
    }
    
    @Override
    public MatchOfficials parseOfficials(String source, CricketMatch match) {
        if (source == null || source.length() == 0) return null;
        String tmp = MyStringUtils.inBetween(source, "<td>", "</td>");
        String[] rows = tmp.split("<br>");
        MatchOfficials mos = new MatchOfficials();
        
        if (rows == null || rows.length == 0) return null;
        
        for (int i=0;i<rows.length;i++) {
            String title = MyStringUtils.inBetween(rows[i], "<b>", "</b>");
            if (title == null || title.trim().length() == 0) continue;
            int pos = rows[i].indexOf("</b>");
            
            Official[] umps;
            if (title.toLowerCase().startsWith("umpire")) {
                Plogger.debug("   finding umpires...");
                umps = this.getOfficials(rows[i].substring(pos + 4));
                if (umps != null) {
                    mos.setUmpires(umps);
                    Plogger.debug("   ok");
                } else {
                    Plogger.warn("   not found!");
                }
            } else if (title.toLowerCase().indexOf(" referee") >= 0) {
                Plogger.debug("   finding referee...");
                umps = this.getOfficials(rows[i].substring(pos + 4));
                if (umps != null && umps.length > 0) {
                    mos.setReferee(umps[0]);
                    Plogger.debug("   ok");
                } else {
                    Plogger.warn("   not found!");
                }
            } else if (title.toLowerCase().startsWith("reserve") || title.toLowerCase().startsWith("4th")) {
                Plogger.debug("   finding reserved umpires..");
                umps = this.getOfficials(rows[i].substring(pos + 4));
                if (umps != null && umps.length > 0) {
                    mos.setReservedUmpire(umps[0]);
                    Plogger.debug("   ok");
                } else {
                    Plogger.warn("   not found!");
                }
            } else if (title.toLowerCase().startsWith("tv") || title.toLowerCase().startsWith("3rd") || title.toLowerCase().startsWith("third")) {
                Plogger.debug("   finding tv umpire..");
                umps = this.getOfficials(rows[i].substring(pos + 4));
                if (umps != null && umps.length > 0) {
                    mos.setTvUmpire(umps[0]);
                    Plogger.debug("   ok");
                } else {
                    Plogger.warn("   not found!");
                }
                
            } 
        
        }
        
        return mos;
    }
    
    private Official[] getOfficials(String source) {
        ArrayList<Official> offs = new ArrayList<Official>();
        source = source.replace("\n", "");
        
        String[] names = MyStringUtils.splitInBetween(source, "<a ", "</a>");
        if (names != null) {
            for (int i=0;i<names.length;i++) {
                names[i] = "<a " + names[i];
                Official off = new Official();
                
                int pos = names[i].indexOf(">");
                HashMap<String, String> attribs = HTMLStringUtils.getHTMLAttributes(names[i].substring(0, pos+1));
                off.setId(this.urlIdParser.parseUrlID(attribs.get("href")));
                off.setShortName(this.urlTitleParser.parseUrlTitle(attribs.get("title")));
                offs.add(off);
            }
        }
        
        String name = HTMLStringUtils.cleanHTMLTags(source);
        
        Player[] players = this.simpleNameParser.parserNames(name);
        if (players.length == offs.size()) {
            for (int i=0;i<offs.size();i++) {
                Official off = offs.get(i);
                off.setCountry(players[i].getCountry());
                off.setTitleName(players[i].getTitleName());
            }
        }
        
        Official[] ofs = new Official[offs.size()];
        offs.toArray(ofs);
        return ofs;
    }
    
    public void setUrlTitleParser(URLTitleParsable urlTitleParser) {
        this.urlTitleParser = urlTitleParser;
    }
    
    public void setUrlIdParser(URLIDParsable urlIdParser) {
        this.urlIdParser = urlIdParser;
    }
    
    public URLIDParsable getUrlIdParser() {
        return urlIdParser;
    }
    
    public URLTitleParsable getUrlTitleParser() {
        return urlTitleParser;
    }
    
    public void setSimpleNameParser(SimpleNameParsable simpleNameParser) {
        this.simpleNameParser = simpleNameParser;
    }
    
    public SimpleNameParsable getSimpleNameParser() {
        return simpleNameParser;
    }
    
    
}
